package com.example.administrator.newjiajiesong.test;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.support.annotation.IdRes;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.util.TypedValue;
import android.widget.Toast;

import com.example.administrator.newjiajiesong.R;
import com.example.administrator.newjiajiesong.widget.TitleBar;


/**
 * 日期：2017.01.03
 * <p>
 * 作者：xudiwei
 * <p>
 * 描述：BaseFragment添加了相关常用的方法。
 */
public class BaseFragment extends Fragment {

    private static final String TAG = "BaseFragment";
    protected Context mContext;
    protected Activity mActivity;
    private ProgressDialog mProgressDialog;
    private boolean isVisible;

    public BaseFragment() {

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
        mActivity = getActivity();
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && !isVisible) {
            isVisible = true;
            onVisible();
        } else {
            if (isVisible) {
                isVisible = false;
                onInvisible();
            }
        }
        Log.d(TAG, "setUserVisibleHint: " + isVisibleToUser);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isVisible) {
            isVisible = true;
            onVisible();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (isVisible) {
            isVisible = false;
            onInvisible();
        }
    }


    /**
     * Fragment可见时
     */
    protected void onVisible() {
        Log.d(TAG, "onVisible");
    }

    /**
     * Fragment不可见时
     */
    protected void onInvisible() {
        Log.d(TAG, "onInvisible");
    }

    /**
     * toast
     *
     * @param msg 吐丝的内容
     */
    public void toast(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * toast
     *
     * @param ids 吐丝的内容的字符串ids
     */
    public void toast(@StringRes int ids) {
        toast(getString(ids));
    }


    /**
     * 线程安全的toast
     *
     * @param msg
     */
    public void toastThreadSafe(final String msg) {
        if (null != mActivity) {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    toast(msg);
                }
            });
        }
    }

    /**
     * 线程安全的toast
     *
     * @param ids
     */
    public void toastThreadSafe(@StringRes final int ids) {
        if (null != mActivity) {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    toast(ids);
                }
            });
        }
    }

    /**
     * 开启一个Activity
     *
     * @param clz 需要开启的Activity
     */
    public void startActivity(Class<? extends Activity> clz) {
        if (null != mContext) {
            startActivity(new Intent(mContext, clz));
//            getActivity().overridePendingTransition(R.anim.trans_next_in,R.anim.trans_next_out);
        }
    }

    /**
     * 开启一个Activity,并finish掉当前的Activity
     *
     * @param clz
     */
    public void startActivityAndFinishSelf(Class<? extends Activity> clz) {
        if (null != mActivity) {
            startActivity(clz);
            mActivity.finish();
        }

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    /**
     * 初始化进度对话框
     */
    protected void showProgressDialog(String dialogMessage) {
        mProgressDialog = new ProgressDialog(mContext);
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.setMessage(dialogMessage);
        mProgressDialog.show();
    }
//
//    @Override
//    public void onHiddenChanged(boolean hidden) {
//        super.onHiddenChanged(hidden);
//        Log.d(TAG, "onHiddenChanged: "+hidden);
//        isVisible = !hidden;
//        if(isVisible){
//            onVisible();
//        }else {
//            onInvisible();
//        }
//    }

    /**
     * 关闭进度对话框
     */
    protected void dismissProgressDialog() {
        if (null != mProgressDialog && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    /**
     * 初始化刷新控件的参数
     *
     * @param ids 刷新控件的id
     */
    protected SwipeRefreshLayout setSwipeRefreshLayout(@IdRes int ids) {
        SwipeRefreshLayout mRefreshLayout = (SwipeRefreshLayout) getView().findViewById(ids);
        mRefreshLayout.setProgressViewOffset(false, 0,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics()));
        mRefreshLayout.setColorSchemeResources(R.color.colorGreen);
        return mRefreshLayout;
    }

    /**
     * 设置标题栏参数
     *
     * @param titleIds              标题栏的id
     * @param title                 标题
     * @param goneLeftBtn           左边按钮是否显示
     * @param goneRightBtn          右边按钮是否显示
     * @param titleBarClickListener 左右边的按钮点击事件回调接口
     * @return 返回设置好的titlebar
     */
    public TitleBar setTitleBar(@IdRes int titleIds, String title
            , boolean goneLeftBtn, boolean goneRightBtn, TitleBar.OnTitleBarClickListener titleBarClickListener) {
        TitleBar titleBar = (TitleBar) getView().findViewById(titleIds);
        titleBar.setTitle(title);
        titleBar.visibleIvLeftBtn(goneLeftBtn);
        titleBar.visibleIvRightBtn(goneRightBtn);
        titleBar.setOnTitleBarClickListener(titleBarClickListener);
        return titleBar;
    }

    /**
     * 隐藏键盘
     */
    public void hideKB() {
//        if (null != mContext && mContext instanceof BaseActivity) {
//            ((BaseActivity) mContext).hintKb();
//        }
    }

}
